import logging

'''
    @author:gaofei
    描述：写入日志类 logging_gf
        创建实例对象，传入写入日志文件的路径path，用户名name默认：user
        分别调用debug,info,warning,error,critical方法，写入日志信息

'''
class Logging_gf:
    def __init__(self , path , name = 'user'):
        self.path = path
        self.name = name

        #实例logger对象
        self.logger = logging.getLogger()

        #添加文件打印位置，如果是打印在文件中，导入文件路径
        fh = logging.FileHandler(self.path, encoding='utf-8')
        ch = logging.StreamHandler() #显示到控制台上

        #添加输出格式
        fm = logging.Formatter('%(asctime)s %(filename)s %(lineno)d ' + self.name + '-->' + "%(message)s")

        #每个输出位置的handler处理器都给定固定的输出格式
        fh.setFormatter(fm)
        ch.setFormatter(fm)

        # 给实例的logger对象添加输出位置的handler处理器
        self.logger.addHandler(fh)
        self.logger.addHandler(ch)

        #设置logger级别,默认级别DEBUG，兼容所有级别
        self.logger.setLevel('DEBUG')

    def debug(self,msg):
        self.logger.debug('DEBUG:' + msg)
    def info(self,msg):
        self.logger.info('INFO:' + msg)
    def warning(self,msg):
        self.logger.warning('WARNING:' + msg)
    def error(self,msg):
        self.logger.error('ERROR:' + msg)
    def critical(self,msg):
        self.logger.critical('CRITICAL:' + msg)

#test测试
if __name__ == '__main__':
    logger = Logging_gf('file_test/logger_test.log')
    logger.debug('gaofei')
    logger.info('gaofei')
    logger.warning('gaofei')
    logger.error('gaofei')
    logger.critical('gaofei')

